package gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import mvc.bezier.ControllerBezier3D;
import mvc.bezier.ModelBezier;
import mvc.elicoide.ModelElicoide;
import mvc.enhancedElicoide.IModelComplexHelix;
import mvc.enhancedElicoide.ModelComplexHelix;
import mvc.enhancedElicoide.ViewEnhanchedElicoide3D;
import mvc.view.ViewCurve;

import org.geom.curve.bezier.Bezier;
import org.geom.curve.helix.Elicoide;
import org.geom.curve.helix.EnhancedElicoide;
import org.gui.MainWindow;
import org.render.CurveRenderer3D;


public class ShuffledHelixMainWindow extends MainWindow { private static final long serialVersionUID = 1L;

	//---------------------------------------------------------------------- class vars

	Dimension viewDimension = new Dimension(330,500);
	static ModelComplexHelix modelComplex;

	public ShuffledHelixMainWindow(String string) {
		super(string, getModel());		
		
		System.out.println("ShuffledHelixMainWindow > .");
		init();
	}

	private static  ModelComplexHelix getModel() {
		if (modelComplex==null) {
			ModelBezier mb = new ModelBezier(new Bezier(4));
			ModelElicoide me = new ModelElicoide(new Elicoide());
			modelComplex = new ModelComplexHelix(mb, me);
		}
		return modelComplex;
	}

	protected void init() {
		
		System.out.println("ShuffledHelixMainWindow > init");
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        
		//setLayout(new GridLayout(2, 1, 5, 5));
		//GridBagLayout g = new GridBagLayout();
        //setLayout(g);
		//setLayout(new FlowLayout());
		setLayout(new GridLayout(1,1, 1, 1));
		
        MainWindow.WIDTH = 1000;
        MainWindow.HEIGHT = 650;
        setSize(WIDTH,HEIGHT);
        
        initViews();
        initControlPanel();
	}
	
	private void initControlPanel() {
		
		JFrame controls = new ControlsPanel(modelComplex);
		controls.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		controls.pack();
		controls.setVisible(true);
		controls.setLocation(1000, 0);
	}

	private void initViews() {
		
		JTabbedPane mainTabber = new JTabbedPane();
		JPanel elicoide = new JPanel();
		JPanel bezier = new JPanel();
		JPanel tredi = new JPanel();
		mainTabber.addTab("Helix", elicoide);
		mainTabber.addTab("Bezier", bezier);
		mainTabber.addTab("Total view", tredi);
		mainTabber.setSize(WIDTH,HEIGHT);
		add(mainTabber);
		
		Dimension d = viewDimension;
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		elicoide.add(getElicoideCluster().setTranslate(d.width/3, d.height/3, true), c);
		
		c.gridx = 0;
		c.gridy = 1;
		bezier.add(getBezierCurveCluster().setTranslate(d.width/3, d.height/3, true), c);
		
		c.gridx = 1;
		c.gridy = 0;
		c.gridheight = 2;
		c.fill = GridBagConstraints.VERTICAL;
		c.insets = new Insets(5, 15, 5, 15);
		tredi.add(getView3D().setTranslate(130, -150, true), c);
						
	}

	private ViewCurve getView3D() {
		ViewCurve viewCurve = new ViewEnhanchedElicoide3D(
				curve, ((IModelComplexHelix)curve).getTrunkModel(),
				new CurveRenderer3D(curve, viewDimension),
				new Dimension(900, 500),
				"complete view"
				);
		ControllerBezier3D controller = new ControllerBezier3D(curve, viewCurve, 
				EnhancedElicoide.U);
		System.out.println("ShuffledHelicMainWindows > anti warning: "+ controller);
		return viewCurve;
	}


	private GuiBlock getBezierCurveCluster() {
		return BlockFactory.getBezierNormalBlock(
				((IModelComplexHelix)curve).getTrunkModel(), 
				viewDimension);
	}
	
	private GuiBlock getElicoideCluster() {
		return BlockFactory.getElicoideSpecialBlock(
				curve, 
				viewDimension);
	}
}
